<template>
  <Dialog v-model="dialogVisible" :init-fullscreen="false" :width="1440">
    <template #title>
      <div
        >工序汇报明细（工序名称：{{ processInfo.name }}，负责人员：{{
          processInfo.userName
        }}，计费方式：{{ processInfo.type }}）</div
      >
    </template>
    <ContentWrap>
      <!-- 列表 -->
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="#" align="center" type="index" width="50" />
        <el-table-column label="操作" align="center" width="120">
          <template #default="scope">
            <el-button link type="primary" @click="openForm(scope.row)"> 修改 </el-button>
            <el-button link type="danger" @click="handleDelete(scope.row.id)"> 删除 </el-button>
          </template>
        </el-table-column>
        <el-table-column label="加工人员" align="center" prop="userName" width="100" />
        <!-- <el-table-column label="检验人员" align="center" prop="checkUserId" width="100" /> -->
        <el-table-column label="合格数量" align="center" prop="passCount" width="100" />
        <el-table-column label="报废数量" align="center" prop="scrapCount" width="100" />
        <el-table-column
          label="加工开始时间"
          align="center"
          prop="workBeginTime"
          :formatter="dateFormatter2"
          width="180"
        />
        <el-table-column
          label="加工结束时间"
          align="center"
          prop="workEndTime"
          :formatter="dateFormatter2"
          width="180"
        />
        <el-table-column label="合计用时(小时)" align="center" prop="allTime" width="120" />
        <el-table-column label="备注" align="center" prop="remark" min-width="150" />
      </el-table>

      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>

  <!-- 表单弹窗：添加/修改 -->
  <ProducePlanProcessItemsForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter2 } from '@/utils/formatTime'
import { ProduceProcessItems, ProduceProcessItemsApi } from '@/api/erp/produce/process/items'
import ProducePlanProcessItemsForm from './ProducePlanProcessItemsForm.vue'

/** ERP 生产计划工序项 列表 */
defineOptions({ name: 'ProducePlanProcessItemsIndex' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const loading = ref(false) // 列表的加载中
const list = ref<ProduceProcessItems[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数

// 工序基本信息
const processInfo = ref({
  name: '',
  userName: '',
  type: '',
  price: '',
  processId: undefined
})

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  process: undefined
})

/** 打开弹窗 */
const open = async (processRow: any) => {
  dialogVisible.value = true

  // 设置工序基本信息
  processInfo.value = {
    name: processRow.name || '',
    userName: processRow.userName || '',
    type: processRow.type || '',
    price: processRow.price || '',
    processId: processRow.id
  }

  queryParams.process = processRow.id
  await getList()
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProduceProcessItemsApi.getProduceProcessItemsPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (row?: any) => {
  const processRow = {
    id: processInfo.value.processId,
    name: processInfo.value.name,
    userId: processInfo.value.userName,
    type: processInfo.value.type,
    price: processInfo.value.price
  }

  if (row && row.id) {
    // 修改
    formRef.value.open(processRow, 'update', row.id)
  } else {
    // 新增
    formRef.value.open(processRow, 'create')
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProduceProcessItemsApi.deleteProduceProcessItems(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>
